from typing import List

from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_purchase.entity.do.price_do import PurchaseDailyPrice
from module_purchase.entity.vo.price_vo import PriceModel, PricePageQueryModel
from utils.page_util import PageUtil


class PriceDao:
    """
    每日采购价记录模块数据库操作层
    """

    @classmethod
    async def get_price_detail_by_id(cls, db: AsyncSession, record_id: int):
        """
        根据飞书记录ID获取每日采购价记录详细信息

        :param db: orm对象
        :param record_id: 飞书记录ID
        :return: 每日采购价记录信息对象
        """
        price_info = (
            (
                await db.execute(
                    select(PurchaseDailyPrice)
                    .where(
                        PurchaseDailyPrice.purchase_price_id == record_id
                    )
                )
            )
            .scalars()
            .first()
        )
        return price_info

    @classmethod
    async def get_price_detail_by_info(cls, db: AsyncSession, price: PriceModel):
        """
        根据每日采购价记录参数获取每日采购价记录信息
        :param db: orm对象
        :param price: 每日采购价记录参数对象
        :return: 每日采购价记录信息对象
        """
        price_info = (
            (
                await db.execute(
                    select(PurchaseDailyPrice).where(
                        PurchaseDailyPrice.purchase_price_id == price.purchase_price_id if price.purchase_price_id else True,
                    )
                )
            )
            .scalars()
            .first()
        )

        return price_info

    @classmethod
    async def get_price_list(cls, db: AsyncSession, query_object: PricePageQueryModel, is_page: bool = False):
        """
        根据查询参数获取每日采购价记录列表信息
        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 每日采购价记录列表信息对象
        """
        product_size_list = query_object.product_size
        query = (
            select(PurchaseDailyPrice)
            .where(
                PurchaseDailyPrice.year == query_object.year if query_object.year else True,
                PurchaseDailyPrice.price == query_object.price if query_object.price else True,
                PurchaseDailyPrice.purchase_date == query_object.purchase_date if query_object.purchase_date else True,
                PurchaseDailyPrice.product_size.in_(product_size_list) if product_size_list else True,
                PurchaseDailyPrice.planter_id == query_object.planter_id if query_object.planter_id else True,
                PurchaseDailyPrice.product_area_id == query_object.product_area_id if query_object.product_area_id else True,
            )
            .order_by(PurchaseDailyPrice.purchase_price_id)
            .distinct()
        )
        price_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)
        return price_list


    @classmethod
    async def add_price_dao(cls, db: AsyncSession, price: PriceModel):
        """
        新增每日采购价记录数据库操作

        :param db: orm对象
        :param price: 每日采购价记录对象
        :return:
        """
        db_price = PurchaseDailyPrice(**price.model_dump())
        db.add(db_price)
        await db.flush()

        return db_price

    @classmethod
    async def edit_price_dao(cls, db: AsyncSession, price: dict):
        """
        编辑每日采购价记录数据库操作

        :param db: orm对象
        :param price: 需要更新的每日采购价记录字典
        :return:
        """
        await db.execute(update(PurchaseDailyPrice), [price])

    @classmethod
    async def delete_price_dao(cls, db: AsyncSession, record_id_list: List[str]):
        """
        删除每日采购价记录数据库操作

        :param db: orm对象
        :param record_id_list: 要删除的记录id列表
        :return:
        """
        await db.execute(delete(PurchaseDailyPrice).where(PurchaseDailyPrice.purchase_price_id.in_(record_id_list)))

